#include "WSTexture.h"
#include "WSGlobal.h"

WS_NS_CORE_BEGIN

WSTexture::WSTexture() 
	: _Width(0), _Height(0), _glTexID(0)
{
}

WSTexture::WSTexture(WSImage* pImage)
{
	if (pImage)
	{
		_Width = pImage->GetWidth();
		_Height = pImage->GetHeight();
		glGenTextures(1, &_glTexID);
		glBindTexture(GL_TEXTURE_2D, _glTexID);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _Width, _Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImage->GetData());
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
}

void WSTexture::Use()
{
	glBindTexture(GL_TEXTURE_2D, _glTexID);
}

void WSTexture::Draw(const WSPoint& point)
{
	GLfloat coord[] =
	{
		0.f, 0.f,
		1.f, 0.f,
		0.f, 1.f,
		1.f, 1.f
	};

	GLfloat vertices[] =
	{
		point.x, point.y,
		point.x + _Width, point.y,
		point.x, point.y + _Height,
		point.x + _Width, point.y + _Height
	};

	glBindTexture(GL_TEXTURE_2D, _glTexID);

	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, coord);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

WS_NS_CORE_END